package com.design_mode;

import java.util.Random;

/**
 * Created by dustaple on 8/4/14.
 */
public class VisitorPattern {

    public static void main(String[] args) {

        SubjectStructure.createElement().acceptVisitor(new Visitor());
    }
}

abstract class AbstractElement {
    public abstract void doSomething();

    public void acceptVisitor(Visitor visitor) {
        visitor.visit(this);
    }
}

class ElementOne extends AbstractElement {
    @Override
    public void doSomething() {
        System.out.println("This is element one speak.");
    }
}

class ElementTwo extends AbstractElement {
    @Override
    public void doSomething() {
        System.out.println("This is element two speak.");
    }
}

class Visitor {
    public void visit(AbstractElement element) {
        element.doSomething();
    }
}

class SubjectStructure {
    public static AbstractElement createElement() {
        Random random = new Random();
        int i = random.nextInt(10);
        if (i > 5) {
            return new ElementOne();
        } else {
            return new ElementTwo();
        }
    }
}